package com.client.qiuqiu;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

public class Login_page extends AppCompatActivity {
    private String loginState = "";

    public void setLoginState(String loginState) {
        this.loginState = loginState;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_page);

        //按钮点击事件
        Button loginButn;
        Button registerButn;

        loginButn = (Button) findViewById(R.id.button_login);
        registerButn = (Button) findViewById(R.id.button_register);

        loginButn.setOnClickListener(new login());
        registerButn.setOnClickListener(new register());

        int permission_write = ContextCompat.checkSelfPermission(Login_page.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int permission_read = ContextCompat.checkSelfPermission(Login_page.this,
                Manifest.permission.READ_EXTERNAL_STORAGE);
        if (permission_write != PackageManager.PERMISSION_GRANTED
                || permission_read != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(Login_page.this, "Requesting Permission", Toast.LENGTH_SHORT).show();
            //申请权限，特征码自定义为1，可在回调时进行相关判断
            ActivityCompat.requestPermissions(Login_page.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }
    }

    class  login implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Login_page.login_Thread login_Thread = new Login_page.login_Thread();
            login_Thread.start();
            try {
                login_Thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


            if(loginState.equals("false")){
                Toast.makeText(Login_page.this, "Login failed!!", Toast.LENGTH_LONG).show();
            }else {
                //登录成功
                //开启消息接收服务
                startService(new Intent(Login_page.this, MessageService.class));
                //开启文件接收服务
                startService(new Intent(Login_page.this, FileService.class));
                //跳转Menu
                Intent intent = new Intent (Login_page.this, Menu.class);
                startActivity(intent);
                finish();
            }
        }
    }

    class  register implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            //注册页面跳转
            Intent intent = new Intent (Login_page.this, Register_page.class);
            startActivity(intent);
        }
    }

    class login_Thread extends Thread{
        public void run(){
            EditText userID = findViewById(R.id.userID);
            EditText userpwd = findViewById(R.id.userPwd);

            try {
                User user = ((User)getApplicationContext());
                //获取服务器端的输出流
                PrintStream printStream = new PrintStream(user.getSocket().getOutputStream());

                //发送服务端事件
                String msg = "Login";
                printStream.println(msg);
                //发送userID
                msg = userID.getText().toString();
                printStream.println(msg);
                //发送pwd
                msg = userpwd.getText().toString();
                msg = Sha256.sha256(msg);
                printStream.println(msg);

                //接受服务端反馈: 确认登录是否成功
                Scanner scanner = new Scanner(user.getSocket().getInputStream());
                msg = scanner.nextLine();

                if(msg.equals("false")){
                    setLoginState("false");
                    return;
                }

                user.setName(msg);
                user.setId(userID.getText().toString());
                setLoginState("true");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}